package com.linln.modules.system.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.linln.modules.system.dao.CustomMapper;
import com.linln.modules.system.dao.UserAccountDetailMapper;
import com.linln.modules.system.dao.UserPointAccountMapper;
import com.linln.modules.system.dao.WxUserMapper;
import com.linln.modules.system.entity.UserAccountDetail;
import com.linln.modules.system.entity.UserAccountDetailExample;
import com.linln.modules.system.entity.UserPointAccount;
import com.linln.modules.system.entity.WxUser;
import com.linln.modules.system.vo.admins.AdminUserPointVo;
import com.linln.modules.system.vo.admins.PointDetailOverBillVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PointsServices {

    @Autowired
    private CustomMapper customMapper;

    @Autowired
    private UserAccountDetailMapper accountDetailMapper;

    public Page<AdminUserPointVo> getUserPointVoPager(Integer pageNum,Integer pageSize,String nickName){
        PageHelper.startPage(pageNum,pageSize,true);
        Page<AdminUserPointVo> vos = customMapper.getUserPointVoPager(nickName);
        if(vos.getTotal()>0){
            List<AdminUserPointVo> result = vos.getResult();

            result.forEach(vo ->{
                int states = vo.getStates();
                String accountId = vo.getId();
                Integer hadPoints = customMapper.getUserHadPoints(accountId);
                if(hadPoints!=null&&hadPoints>=0){
                    vo.setHadPoints(0);
                }else if(hadPoints!=null && hadPoints<0){
                    vo.setHadPoints(-hadPoints);
                }
                if(states==0){
                    vo.setStatus("正常");
                }
                if(states==1){
                    vo.setStatus("已冻结");
                }
            });
        }
        return vos;
    }


    public List<UserAccountDetail> getAccountDetail(String userId,String accountId){


        UserAccountDetailExample example = new UserAccountDetailExample();
        example.createCriteria().andUserIdEqualTo(userId).andAccountIdEqualTo(accountId);

        List<UserAccountDetail> list = accountDetailMapper.selectByExample(example);
        if (!list.isEmpty()) {
            list.forEach( acd ->{
                Integer kinds = acd.getKinds();
                if (kinds == 0) {
                    acd.setRemark("消费");
                } else if (kinds == 1) {
                    acd.setRemark("新用户注册赠送积分");
                } else if (kinds == 2) {
                    acd.setRemark("消费凭证兑换");
                }
            });
            return list;
        }
        return null;
    }


    public List<PointDetailOverBillVo> getAllDetail(String userId){
        ArrayList<PointDetailOverBillVo> detail = new ArrayList<>();
        List<PointDetailOverBillVo> vos = customMapper.userOrder1(userId);
        List<PointDetailOverBillVo> vos1 = customMapper.userAccountDetail1(userId);
        if (!vos.isEmpty()) {
            vos.forEach(a ->{
                int kinds = a.getKinds();
                if(kinds==0){
                    a.setStrKind("预约服务");
                }
                if (kinds==1){
                    a.setStrKind("兑换商品");
                }
                int points = a.getPoints();
                a.setPoints(-points);
            });
        }
        detail.addAll(vos);
        if (!vos1.isEmpty()) {
            vos1.forEach(a ->{
                int kinds = a.getKinds();
                if(kinds==2){
                    a.setStrKind("凭证兑换");
                }
                if (kinds==1){
                    a.setStrKind("注册赠送");
                }
                if (kinds==3){
                    a.setStrKind("邀请好友赠送");
                }
                if (kinds==4){
                    a.setStrKind("好友首次消费赠送");
                }
                int points = a.getPoints();
                a.setPoints(+points);
            });
        }
        detail.addAll(vos1);
        if (!detail.isEmpty()) {
            List<PointDetailOverBillVo> collect = detail.stream().sorted(Comparator.comparing(PointDetailOverBillVo::getCreateTime)).collect(Collectors.toList());
            return collect;
        }
        return null;
    }


    @Autowired
    private UserPointAccountMapper pointAccountMapper;

    public int lockAndRelease(String id,int state){
        UserPointAccount account = new UserPointAccount();
        account.setId(id);
        account.setState(state);
        account.setUpdateTime(new Date());
        if (state==0){
            account.setRemark("解冻");
        }
        if (state==1){
            account.setRemark("冻结");
        }

        int i = pointAccountMapper.updateByPrimaryKeySelective(account);
        return i;
    }
}
